package brick.has.service;

import brick.has.entity.Device;
import brick.has.entity.ParameterBoolean;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@Service
public class DeviceService {

    Map<String, Device> deviceMap = new ConcurrentHashMap<>();

    public void report(Device device) {

        deviceMap.put(device.getId(), device);
    }

    public void control(String deviceId, String parameterId, String targetValue) {
        Device device = deviceMap.get(deviceId);
        if (device == null) {
            throw new RuntimeException(String.format("【DeviceId: %s】无效：未查找到设备", deviceId));
        }
        Map<String, ParameterBoolean> parameterBooleanMap = device.getParameterBooleanList().stream()
                .collect(Collectors.toMap(ParameterBoolean::getId, me -> me));
        ParameterBoolean parameterBoolean = parameterBooleanMap.get(parameterId);
        if (parameterBoolean == null) {
            throw new RuntimeException(String.format("【ParameterId: %s】无效：未查找到参数"));
        }
        // todo 发起控制
        String deviceUrl = device.getUrl();
    }

    public Map<String, Device> getDeviceMap() {
        return deviceMap;
    }
}
